package com.application.Activity;

import it.unipr.ce.dsg.s2p.sip.Address;

import java.util.concurrent.CountDownLatch;

import com.application.Controller.ActivityController;
import com.application.Controller.NetworkController;
import com.application.Network.PingThread;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class frmMatKetNoiActivity extends Activity{
	public static boolean isShowed=false;
	public static Runnable raClose;
	Button btnKetNoiLai;
	TextView txtKetNoiLai;
	int soLan=0;
	public void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_mat_ket_noi);
		isShowed=true;
		raClose=new Runnable() {
			
			@Override
			public void run() {
				close();
			}
		};
		soLan=0;
		txtKetNoiLai=(TextView)findViewById(R.id.txtKetNoiLai);
		btnKetNoiLai=(Button)findViewById(R.id.btnKetNoiLai);		
		btnKetNoiLai.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				final ProgressDialog dialog = ProgressDialog.show(frmMatKetNoiActivity.this, "", 
                        "Đang kết nối. Vui lòng chờ...", true);
				
//				if(!PingThread.getIsRunning()){
//					PingThread.getInstance("ket noi lai").start();
//					PingThread.setRunning(true);
//					Log.d("Phu","start ping in mat ket noi");					
//				}
//				NetworkController.setStated(true);
//					final PingThread t=new PingThread("mat ket noi");
//					t.start();
				NetworkController.sendPing(new Address(NetworkController.thanhLapNhomIP));
				CountDownTimer count=new CountDownTimer(10000,1000) {
					
					@Override
					public void onTick(long millisUntilFinished) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onFinish() {						
						if(NetworkController.getStated()){
							NetworkController.sendUpdateTree(new Address(NetworkController.thanhLapNhomIP));
							dialog.dismiss();							
							close();
						}else{
							dialog.dismiss();
							setTextBox();	
							
						}
//						t.setRunning(false);
						Log.d("Phu","cound down finish");
					}
				};
				count.start();
			}
		});
	}
	@Override
	public void onStart(){
		super.onStart();
		ActivityController.setPeerActivity(this);
	}
	@Override
	public void onResume(){
		super.onResume();
		ActivityController.setPeerActivity(this);
	}
	@Override
	public void onPause(){
		super.onPause();
		ActivityController.setPeerActivity(this);
	}
	@Override
	public void onStop(){
		super.onStop();
		isShowed=false;
	}
	public void close(){
		isShowed=false;
		finish();
	}
	private void setTextBox(){
		soLan++;
		txtKetNoiLai.setText("Kết nối lại lần thứ "+soLan+" thất bại!");
	}
}
